package org.ainlolcat.ainscience.entity;

import org.ainlolcat.aincalc2.AinCalc;
import org.ainlolcat.aincalc2.Cleks;
import org.ainlolcat.aincalc2.Leksemma;
import org.ainlolcat.aincalc2.MissingDependenciesException;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.column.ColumnImpl;
import org.ainlolcat.ainscience.column.CustomColumn;
import org.ainlolcat.ainscience.column.threshold.Threshold;
import org.ainlolcat.ainscience.column.threshold.ThresholdHelper;
import org.ainlolcat.ainscience.templates.Template;
import org.ainlolcat.ainscience.tools;
import org.apache.log4j.Logger;
import org.jdom.Element;

import javax.print.attribute.HashAttributeSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

/**
 * @author ainlolcat
 * Date: 12/21/11
 */
public class EntityCustomColumnGenerator {
    private static Logger log = Logger.getLogger(EntityCustomColumnGenerator.class);

    public Entity generateCustomColumns(Entity entity){
        Template template = entity.getTemplate();
        Element parseInfo = template.getEntityParseParams(entity.getType());
        List<Element> custCols = parseInfo.getChild("customColumns")==null?
                                    new ArrayList<Element>():
                                    parseInfo.getChild("customColumns").getChildren("customColumn");
        for (Element ecolumn:custCols){
            handleColumn(entity, ecolumn);
        }
        return entity;
    }

    private void handleColumn(Entity entity, Element ecolumn){
        //TODO write for null id
        String name = ecolumn.getAttributeValue("name");
        String id = ecolumn.getAttributeValue("id");
        String axis = ecolumn.getAttributeValue("axis");
        String className = ecolumn.getAttributeValue("class");
        if (className!=null && entity.getColumnByName(name) == null){
            try {
                Hashtable<String,String> params = new Hashtable<String, String>();
                for (Element param:(List<Element>)ecolumn.getChildren("parameter"))  {
                    params.put(param.getAttributeValue("name"),param.getAttributeValue("value"));
                }
                Class clazz  = Class.forName(className);
                CustomColumn handler = (CustomColumn) clazz.newInstance();
                Column column =  handler.generateColumn(params,entity);

                new ThresholdHelper().searchThresholdAndApply(column, ecolumn);

                if (column!=null){
                    column.setName(name);
                    column.setAxis(axis);
                    entity.setColumn(column, tools.parseInteger(id));
                }
            } catch (MissingDependenciesException ex) {
                throw ex;
            } catch (Exception ex) {
                log.error("Cannot load custom column " + name + " with id " + id + " class " + className,ex);
                //@TODO add resolver. or not add. class resolver is too difficult for user.
            }
        }
    }
}
